package com.huawei.hwid.update.d;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.text.TextUtils;
import android.view.KeyEvent;
import com.huawei.appgallery.assistantdock.base.externalaction.openviewaction.RemoteBuoyAction;
import com.huawei.appmarket.service.deamon.download.remote.DownloadAgentService;
import com.huawei.hwid.core.d.b.e;
import com.huawei.hwid.core.d.l;
import com.huawei.hwid.update.receive.SilentInstallReceive;
import com.huawei.updatesdk.service.otaupdate.UpdateKey;
import com.huawei.updatesdk.service.otaupdate.UpdateStatusCode.DialogButton;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class i extends a {
    private BroadcastReceiver j;
    private Handler k = new Handler();
    private int l = 0;
    private Handler m = new Handler(this, Looper.getMainLooper()) {
        final /* synthetic */ i a;

        public void handleMessage(Message message) {
            super.handleMessage(message);
            Bundle bundle = (Bundle) message.obj;
            switch (message.what) {
                case DialogButton.CONFIRM /*101*/:
                    this.a.a(bundle);
                    return;
                case 102:
                    this.a.b(bundle);
                    return;
                case 103:
                    this.a.c(bundle);
                    return;
                default:
                    return;
            }
        }
    };

    class a implements Runnable {
        final /* synthetic */ i a;

        private a(i iVar) {
            this.a = iVar;
        }

        public void run() {
            this.a.c(14);
        }
    }

    public void a(Activity activity) {
        super.a(activity);
        if (this.c != null) {
            this.f = 0;
            if (!b(activity) && !a(true)) {
                a(8, this.f);
            }
        }
    }

    public void a() {
        this.k.removeCallbacksAndMessages(null);
        h();
        super.a();
    }

    public boolean a(int i, int i2, Intent intent) {
        boolean z = false;
        if (this.e && this.b != null) {
            return this.b.a(i, i2, intent);
        }
        if (i != f()) {
            return false;
        }
        if (i2 == 0) {
            g();
            b(20000);
            return true;
        } else if (i2 == 4) {
            c();
            return true;
        } else {
            if (i2 != 2) {
                z = true;
            }
            if (a(z)) {
                return true;
            }
            a(i2, this.f);
            return true;
        }
    }

    private boolean b(Activity activity) {
        e.b("SilentUpdateWizard", "silentInstall", true);
        if (TextUtils.isEmpty(this.g)) {
            return false;
        }
        Intent intent = new Intent("com.huawei.appmarket.intent.action.ThirdUpdateAction");
        intent.setPackage("com.huawei.appmarket");
        JSONArray jSONArray = new JSONArray();
        JSONObject jSONObject = new JSONObject();
        try {
            jSONObject.put("pkgName", this.g);
            jSONObject.put("versioncode", this.i);
            jSONArray.put(jSONObject);
            intent.putExtra(RemoteBuoyAction.REMOTE_BUOY_PARAM, jSONArray.toString());
            intent.putExtra("isHmsOrApkUpgrade", true);
            if (d() != null) {
                intent.putExtra("buttonDlgY", d().getString(l.a(d(), "CS_install")));
                intent.putExtra("buttonDlgN", d().getString(l.a(d(), "cs_cancel")));
            }
            intent.putExtra("upgradeDlgContent", activity.getString(l.a(activity, "cs_update_message_new"), new Object[]{"%P"}));
            try {
                activity.startActivityForResult(intent, f());
                return true;
            } catch (ActivityNotFoundException e) {
                e.d("SilentUpdateWizard", "ActivityNotFoundException", true);
                return false;
            }
        } catch (JSONException e2) {
            e.d("SilentUpdateWizard", "create hmsJsonObject fail", true);
            return false;
        }
    }

    public void b() {
        super.b();
    }

    private void g() {
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("com.huawei.appmarket.service.downloadservice.Receiver");
        intentFilter.addAction("com.huawei.appmarket.service.downloadservice.progress.Receiver");
        intentFilter.addAction("com.huawei.appmarket.service.installerservice.Receiver");
        this.j = new SilentInstallReceive(this.m);
        Activity d = d();
        if (d != null) {
            d.registerReceiver(this.j, intentFilter);
        }
    }

    private void h() {
        Activity d = d();
        if (d != null && this.j != null) {
            d.unregisterReceiver(this.j);
            this.j = null;
        }
    }

    public int f() {
        return 2000;
    }

    private void b(int i) {
        this.k.removeCallbacksAndMessages(null);
        this.k.postDelayed(new a(), (long) i);
    }

    private void c(int i) {
        this.k.removeCallbacksAndMessages(null);
        h();
        e();
        if (!a(false)) {
            a(i, this.f);
        }
    }

    public void a(int i, KeyEvent keyEvent) {
        super.a(i, keyEvent);
    }

    private void a(Bundle bundle) {
        String str = null;
        if (bundle.containsKey("UpgradePkgName")) {
            str = bundle.getString("UpgradePkgName");
        }
        if (str != null && str.equals(this.g) && bundle.containsKey("downloadtask.status")) {
            int i = bundle.getInt("downloadtask.status");
            if (i == 3 || i == 5 || i == 6 || i == 8) {
                c(i);
            } else if (i == 4) {
                b(60000);
            } else {
                b(20000);
            }
        }
    }

    private void b(Bundle bundle) {
        String str = null;
        if (bundle.containsKey("UpgradePkgName")) {
            str = bundle.getString("UpgradePkgName");
        }
        if (str != null && str.equals(this.g) && bundle.containsKey("UpgradeDownloadProgress") && bundle.containsKey("UpgradeAppName")) {
            int i = bundle.getInt("UpgradeDownloadProgress");
            b(20000);
            if (i >= 99) {
                i = 99;
            }
            this.l = i;
            if (this.d == null) {
                a(e.class);
            }
            if (this.d != null) {
                ((e) this.d).b(i);
            }
        }
    }

    private void c(Bundle bundle) {
        if (bundle.containsKey(DownloadAgentService.PACKAGE_NMAE_ARG) && bundle.containsKey(UpdateKey.STATUS)) {
            String string = bundle.getString(DownloadAgentService.PACKAGE_NMAE_ARG);
            int i = bundle.getInt(UpdateKey.STATUS);
            if (string != null && string.equals(this.g)) {
                if (i == 2) {
                    this.k.removeCallbacksAndMessages(null);
                    if (this.d != null) {
                        ((e) this.d).b(100);
                    }
                    a(0, this.f);
                } else if (i == -1 || i == -2) {
                    c(i);
                } else {
                    b(60000);
                }
            }
        }
    }

    void a(Class<? extends b> cls) {
        try {
            b bVar = (b) cls.newInstance();
            if (this.l > 0 && (bVar instanceof e)) {
                ((e) bVar).a(this.l);
            }
            bVar.a((a) this);
            this.d = bVar;
        } catch (InstantiationException e) {
            e.d("SilentUpdateWizard", "In  InstantiationException", true);
        } catch (IllegalAccessException e2) {
            e.d("SilentUpdateWizard", "In  IllegalAccessException", true);
        } catch (IllegalStateException e3) {
            e.d("SilentUpdateWizard", "In showDialog, IllegalStateException", true);
        }
    }

    void c() {
        a(13, this.f);
    }
}
